package com.aidong.login.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.login.biz.MemberLocationReportBiz;
import com.aidong.login.entity.MemberLocationReport;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 我的位置上传
 * 版本控制
 * 消息通知
 * 客服通知
 * 广告位点击记录
 * @author java
 * @version 1.0    
 *  2015.07.06
 */
@ParentPackage("json")
@Namespace("/")
public class MemberLocationReportAction implements ModelDriven<MemberLocationReport> {

	private MemberLocationReport locationReport;
	
	@Override
	public MemberLocationReport getModel() {
		if(locationReport == null){
			locationReport = new MemberLocationReport();
		}
		return locationReport;
	}
	
	private MemberLocationReportBiz locationReportBiz; // 位置service
	
	private Map<String, Object> map;
	
	public Map<String, Object> getMap() {
		return map;
	}

	@Autowired
	public void setLocationReportBiz(MemberLocationReportBiz locationReportBiz) {
		this.locationReportBiz = locationReportBiz;
	}
	
	/**
	 * 上传我的位置坐标
	 * @return
	 */
	@Action(value="editmylocation",results={@Result(name="succ",type="json",params={"root","map"})})
	public String editMyLocation(){
		map = new HashMap<String, Object>();
		try {
			double latitude = locationReport.getLatitude(); // 纬度
			double longitude = locationReport.getLongitude(); // 经度
			int idongId = locationReport.getIdongId(); // 爱动健身号
			if(latitude > 0 && longitude > 0 && idongId > 0){
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("latitude", latitude);
				paraMap.put("longitude", longitude);
				paraMap.put("idongId", idongId);
				locationReportBiz.editMyLocation(paraMap);
				map.put("code", 1);
				map.put("result", "报告成功");
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * Android版本更新
	 * @return
	 */
	@Action(value="updateversionandroid",results={@Result(name="succ",type="json",params={"root","map"})})
	public String updateVersionAndroid(){
		map = new HashMap<String, Object>();
		try {
			Map<String, Object> androidVersion = locationReportBiz.getAndroidVersion();
			int versionNum = Integer.parseInt(androidVersion.get("versionNum").toString()); // 版本号
			String downLink = androidVersion.get("downLink").toString(); // 下载链接
			String isUpdate = androidVersion.get("isUpdate").toString(); // 是否强制更新 Y：是  N：否
			String loginPeriod = androidVersion.get("loginPeriod").toString();
			map.put("code", 1);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("isUpdate", isUpdate);
			resultMap.put("versionNum", versionNum);
			resultMap.put("downLink", downLink);
			resultMap.put("loginPeriod", loginPeriod);
			map.put("result", resultMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}

	/**
	 * Ios版本更新
	 * @return
	 */
	@Action(value="updateversionios",results={@Result(name="succ",type="json",params={"root","map"})})
	public String updateVersionIos(){
		map = new HashMap<String, Object>();
		try {
			Map<String, Object> iosVersion = locationReportBiz.getIosVersion();
			String versionNum = iosVersion.get("versionNum").toString(); // 版本号
			String isUpdate = iosVersion.get("isUpdate").toString(); // 是否强制更新 Y：是  N：否
			String loginPeriod = iosVersion.get("loginPeriod").toString();
			map.put("code", 1);
			map.put("versionNum", versionNum);
			map.put("isUpdate", isUpdate);
			map.put("loginPeriod", loginPeriod);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 消息通知(我)
	 * @return
	 */
	@Action(value="getmessagenotiful",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMessageNotification() {
		map = new HashMap<String, Object>();
		try {
			int idongId = locationReport.getIdongId();
			if(idongId != 0){
				Map<String, Object> resultMap = locationReportBiz.getMessageNotification(idongId);
				if(resultMap != null){
					String letterFlg = resultMap.get("letterFlg") + "";
					String appointFlg = resultMap.get("appointFlg") + "";
					String orderFlg = resultMap.get("orderFlg") + "";
					
					Map<String, Object> reqMap = new HashMap<String, Object>();
					if("null".equals(letterFlg)){
						reqMap.put("letterFlg", 1);
					}else{
						reqMap.put("letterFlg", letterFlg);
					}
					if("null".equals(appointFlg)){
						reqMap.put("appointFlg", 1);
					}else{
						reqMap.put("appointFlg", appointFlg);
					}
					if("null".equals(orderFlg)){
						reqMap.put("orderFlg", 1);
					}else{
						reqMap.put("orderFlg", orderFlg);
					}
					
					map.put("code", 1);
					map.put("result", reqMap);
					return "succ";
				}
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
		return null;
	}
	
	/**
	 * 客服通知
	 * @return
	 */
	@Action(value="customerserviceinfo",results={@Result(name="succ",type="json",params={"root","map"})})
	public String customerServiceInfo(){
		map = new HashMap<String, Object>();
		try {
			map.put("code", 1);
			map.put("result", "1、爱动健身仅提供因以下原因造成的用户退款：\n"
					+ "(a) 爱动健身明示可予以退款的；\n"
					+ "(b) 爱动健身系统原因造成的用户已完成付款但订单无法生成的（即订单状态为“待支付”或“超时取消”的）；\n"
					+ "(c) 爱动健身合作商户原因造成的订单信息（如排课信息、场次、日期等）变更而使得用户无法正常参加的；\n"
					+ "(d) 因不可抗力或政府管制（如全国哀悼日，国家禁止娱乐活动的）造成用户无法正常参加的；\n"
					+ "2、用户同意以下爱动健身的不予退款的条件及约定：\n"
					+ "(a) 订购成功，已收到支付成功的验证码短信，订单状态以“已支付”为准，此种情况下用户一旦购买成功，爱动健身不接受任何退款或变更；\n"
					+ "(b) 预约时间内，用户未到场或未准时到场；\n"
					+ "(c) 用户遵守爱动健身对退款的相关规定，并允许爱动健身保留对规定更新或更改的权利；\n"
					+ "(d) 若有特殊情况不能参加活动或课程，需在活动或课程开始至少提前一天的18:00前拨打客服电话进行退款（因为名额有限，您的报名可能导致其他用户不能报名，所以退款需要提前至少一天），逾时不予退款；\n"
					+ "(e) 营养品，在预定的取货时间后48小时未领取的，系统自动退款；\n"
					+ "(f) 票务一经售出不予退款；\n\n"
					+ "工作时间：周一至周五（09:00-18:00）\n\n"
					+ "非工作时间可至客服邮箱:service@aidong.me，爱动客服人员会在工作时间及时回复处理相关问题；\n"
					+"请在邮箱中写明问题内容及其相关信息，如【退订单】:手机号、订单号/预约号、购买的产品名称、数量、场馆名称、退货原因；");
			map.put("phone", "021-54730971转838");
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 广告位点击记录
	 * @return
	 */
	@Action(value="addadvertising",results={@Result(name="succ",type="json",params={"root","map"})})
	public String mstAdvertisingClick(){
		map = new HashMap<String, Object>();
		try {
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("idongId", locationReport.getIdongId());
			paraMap.put("recmdId", locationReport.getRecmdId());
			locationReportBiz.mstAdvertisingClick(paraMap);
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 1);
			map.put("msg", "系统异常");
			return "succ";

		}
	}
	
	/**
	 * 打开APP广告
	 * @return
	 */
	@Action(value="getopenaidongappav",results={@Result(name="succ",type="json",params={"root","map"})})
	public String openAidongAppAv(){
		map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> opensMap = locationReportBiz.getOpenAidongAppAv();
			for (Map<String, Object> open : opensMap) {
				open.put("imageUrl", ReadPropertiesUtil.getIMGREAD(open.get("imageUrl")));
			}
			map.put("code", 1);
			map.put("result", opensMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}
